package view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import controller.MatriceDocMot;

public class JMatriceDocMot extends JFrame{
	JComboBox corpus = new JComboBox();
	JComboBox textes = new JComboBox();
	JButton runAlgo = new JButton("Construire Matrice");
	JButton valider_corpus = new JButton("Valider Corpus");
	DefaultTableModel model ;
    Object[][] datas;
	JTable matrice = new JTable();
	DefaultTableModel modelTF ;
    Object[][] datasTF;
	JTable matriceTF = new JTable();
	
	List<String> pathCorpus = new ArrayList<String>();
	List<String> pathTextes = new ArrayList<String>();
	
	public JMatriceDocMot() {
		super();
		init();
		// GridLayout
		setLayout(new GridLayout(3,1));
		add(TopPanel());
		model = (DefaultTableModel) matrice.getModel();
	     model.addColumn("Mots");
	     model.addColumn("Fréquences");
	     JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	     pane.setViewportView(matrice);
	     matrice.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	     add(pane);
	    /*--------------------------------------------------*/
	     modelTF = (DefaultTableModel) matriceTF.getModel();
	     modelTF.addColumn("Mots");
	     modelTF.addColumn("Fréquences");
	     JScrollPane paneTF = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	     paneTF.setViewportView(matriceTF);
	     matriceTF.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	     add(paneTF);
	     /*--------------------------------------------------*/
	     File folder = new File("fichiersOriginaux");
	     for(File f : folder.listFiles()){
	    	 if(!f.getName().equals(".DS_Store")){
	    		 corpus.addItem(f.getName());
	    		 pathCorpus.add(f.getAbsolutePath());
	    	 }
	     }
		
		setVisible(true);
		
		
	}
	
	public void init() {
		setTitle("Matrice Documents/Mots");
	    setSize(new Dimension(470, 400));
	    setLocationRelativeTo(null);
	}
	
	public JPanel TopPanel() {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(2,2));
		res.add(corpus);
		JPanel buttons = new JPanel();
		buttons.setLayout(new GridLayout(1,1));
		buttons.add(valider_corpus);
		valider_corpus.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				textes.removeAllItems();
				pathTextes = new ArrayList<String>();
				File folder = new File(pathCorpus.get(corpus.getSelectedIndex()));
				File[] listOfFiles = folder.listFiles();
				textes.addItem("*");
				for(File f : listOfFiles){
					if(f.isFile() && !f.getName().equals(".DS_Store")){
						textes.addItem(f.getName());
						pathTextes.add(f.getAbsolutePath());
					}
				
			}}});
		res.add(buttons);
		res.add(textes);
		res.add(runAlgo);
		runAlgo.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int choix = textes.getSelectedIndex();
				System.out.println(choix);
				switch(choix){
					case 0:
						for(int in=0; in<model.getRowCount();in++){
							model.removeRow(in);
						}
						
						Map<String, Map<String, Integer>> resCorpus;
						Map<String, Map<String, Double>> resCorpusTF;
						
					try {
						resCorpus = MatriceDocMot.matriceDocMotCorpusController(corpus.getSelectedItem().toString(), 0);
						resCorpusTF = MatriceDocMot.matriceTFTDF(resCorpus);

						List<String> words = new ArrayList<String>();
						// on récupère TOUS les mots présents sur toutes les map
						for(String key : resCorpus.keySet()){
							for(Entry<String, Integer> de : resCorpus.get(key).entrySet())
								words.add(de.getKey());
						}
						Object[] data = new Object[words.size()+1];
						data[0]= ". / .";
						int index = 1;
						for(String word : words){
							data[index++]=word;
						}
						// premier table
						model.setColumnIdentifiers(data);
						// second table
						modelTF.setColumnIdentifiers(data);
						
						//remplir premiere table
						datas = new Object[resCorpus.size()+1][words.size()];
						index = 0;
						for(String key : resCorpus.keySet()){
							datas[index][0] = key;
							for(Entry<String, Integer> de : resCorpus.get(key).entrySet()){
								int i = words.indexOf(de.getKey())+1;
								datas[index][i]=de.getValue(); 
							}
							for(int i = 0; i < words.size();i++){
								if(datas[index][i] == null)
									datas[index][i]=0;
								
							}
						model.addRow(datas[index]);
						index++;
						}
						//remplir deuxieme table
						datasTF = new Object[resCorpusTF.size()+1][words.size()];
						index = 0;
						for(String key : resCorpusTF.keySet()){
							datasTF[index][0] = key;
							for(Entry<String, Double> de : resCorpusTF.get(key).entrySet()){
								int i = words.indexOf(de.getKey()+1);
								datasTF[index][i]=de.getValue(); 
							}
							for(int i = 0; i < words.size();i++){
								if(datasTF[index][i] == null)
									datasTF[index][i]=0;
								
							}
						modelTF.addRow(datasTF[index]);
						index++;
						}
						
						
					} catch (IOException e1) {}
						break;
					default:
						for(int in=0; in<model.getRowCount();in++){
							model.removeRow(in);
						}
					Map<String, Integer> res = new HashMap<String, Integer>();
					try {
						res = MatriceDocMot.matriceDocMotFichierController(pathTextes.get(choix-1), 0);
						Object[] data = new Object[res.size()+1];
						datas = new Object[1][res.size()+1];
						data[0] = "titre";
						datas[0][0] = textes.getSelectedItem();
						
						int index = 1;
						for (String key : res.keySet())
	                    {
							datas[0][index]= res.get(key);
							data[index] = key;
							++index;
	                    }
						model.setColumnIdentifiers(data);
						model.addRow(datas[0]);
					} catch (IOException e1) {}
						
						break;
				
				
				}
			}});
		
		
		
		return res;
		
		
		
	}
	

}
